package main

import (
	"github.com/gin-gonic/gin"
)

func done(c *gin.Context, code int, data any) {
	if _, ok := data.(string); ok {
		c.JSON(code, gin.H{
			"code": code,
			"msg":  data,
		})
	}
	c.JSON(code, data)
}

func init() {
	SqliteDB()
}

func main() {
	// gin.SetMode(gin.ReleaseMode)
	router := gin.Default()
	RegisterRouter(router, &WantRouter{}, &AnswerRouter{})

	// curl -X GET http://localhost:8080/wants
	router.GET("/wants", func(c *gin.Context) {
		var wants []Want
		SqliteDB().Find(&wants)
		for i := 0; i < len(wants); i++ {
			SqliteDB().Where("want_id = ?", wants[i].ID).Find(&wants[i].Answers)
		}
		c.JSON(200, &wants)
	})

	// curl -X GET http://localhost:8080/answers
	router.GET("/answers", func(c *gin.Context) {
		var answers []Answer
		SqliteDB().Find(&answers)
		c.JSON(200, &answers)
	})

	router.Run(":8080")
}

// gin --port 3000 --appPort 8080 run .
// 启动一个虚拟机服务，监听3000端口，转到内部8080端口
// eg. curl -X GET http://localhost:3000/wants
